#登录操作类
import time
from selenium.webdriver.firefox.service import Service
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By


class Login_Operation:
    #登录操作
    # def login(self,driver,username,password):
    #     driver.find_element(By.ID,"username").send_keys(username)
    #     driver.find_element(By.ID,"password").send_keys(password)
    #     driver.find_element(By.ID,"login_btn").click()
    #     time.sleep(1)

    @pytest.fixture(scope="module",params=["chrome","firefox"])
    def login_driver(self,request):
        if request.param == "chrome":
            driver = webdriver.Chrome()
        elif request.param == "firefox":
            fire_browser_path = r"D:\FIREFOX\firefox.exe"
            options = webdriver.FirefoxOptions()
            options.binary_location = fire_browser_path
            browser_driver = Service('D:\FIREFOX\geckodriver.exe')
            driver = webdriver.Firefox(service=browser_driver, options=options)
        else:
            raise ValueError("浏览器类型错误")
        driver.maximize_window()
        yield driver
        driver.quit()

    @pytest.fixture(scope="function")
    def login_page(self, login_driver, username, password):
        login_driver.get("http://localhost:90/")
        login_driver.find_element(By.ID, "username").send_keys(username)
        login_driver.find_element(By.ID, "password").send_keys(password)
        login_driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
    #固定登录
    def logins(self,login_driver):
        username = "lisi"
        password = "123456"
        login_driver.get("http://localhost:90/")
        login_driver.find_element(By.ID, "username").send_keys(username)
        login_driver.find_element(By.ID, "password").send_keys(password)
        login_driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
    #切换登录
    def switch_login(self,login_driver,username,password):
        login_driver.get("http://localhost:90/")
        time.sleep(1)
        login_driver.find_element(By.ID, "username").send_keys(username)
        login_driver.find_element(By.ID, "password").send_keys(password)
        login_driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
    #判定与断言
    def assserttitle(self,login_driver,result,expect):
        if result == expect:
            print("通过")
        else:
            print("不通过")
            import os
            if not os.path.exists("demo"):
                os.mkdir("demo")
            login_driver.save_screenshot("demo/lisi---123456.png")
        assert result == expect

    #定位标签
    def getSuccessResult(self,login_driver):
        time.sleep(1)
        return login_driver.title
